define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data("area", ['950px','600px']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cooperate/cooperation/index' + location.search,
                    add_url: 'flow/cooperation/add?ids=1',
                    edit_url: 'cooperate/cooperation/edit?ids={bizobjectid}',
                    del_url: 'cooperate/cooperation/del',
                    multi_url: '',
                    import_url: '',
                    table: 'adjust_apply',
                }
            });

            var table = $("#table");

            //获取牵头部门数组列表
            let departmentList = {};
            $.ajax({
                url : "department/index/selectpage",
                async : false,
                success : function (obj) {
                    for(let i=0;i<obj.total;i++){
                        departmentList[obj.rows[i].id] = obj.rows[i].name;
                    }
                    //console.log(costList);
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'createtime',
                sortOrder: "desc",
				pk: 'id',
                search: false,
                showToggle:false,
                showColumns:false,
				searchFormVisible: true,
                exportTypes:['excel'],
                exportOptions:{
                    fileName:'合作项目表',
                    ignoreColumn:[-1]
                },
                undefinedText:'',
                onLoadSuccess: function (data) {
                    var data = $('#table').bootstrapTable('getData', true);
                    //合并单元格
                    Controller.api.mergeCells(data, ["instancecode"], 1, $('#table'));
                },
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'instancecode',
                            title: __('流水号'),
                            operate: "LIKE",
                            formatter: Controller.api.formatter.browser,
                            events: Controller.api.events.browser
                        },
                        {field: 'cname', title: __('协议名称'), operate: 'like'},
                        {field: 'initiator_id', title: __('发起人'), operate: false },
                        {field: 'jiafang', title: __('甲方'), operate: 'like'},
                        {field: 'partner', title: __('乙方'), operate: 'like'},
                        {field: 'cooperation.department_id', title: __('牵头部门'),visible: false,searchList:departmentList,formatter: Table.api.formatter.normal},
                        {field: 'department_name', title: __('牵头部门'), operate: false},
						 {field: 'type', title: __('协议类型'), formatter: Controller.api.formatter.type, searchList: [{ id: 1, name: '校地合作' }, { id: 2, name: '校企合作' },{ id: 3, name: '校校合作' },{ id:4, name: '其他' }] },
                         { field: 'id', title: __('id'), visible: false, operate: false },
                        //{ field: 'instanceid', title: __('instanceid'), visible: false, operate: false },
                        //{ field: 'bizscheme', title: __('bizscheme'), visible: false, operate: false },
                        //{ field: 'flowcode', visible: false, operate: false },
                        //{ field: 'flowname', title: '流程名称',operate: "LIKE" },
                        //{ field: 'stepid', visible: false, operate: false },
                        //{ field: 'stepname', title: '步骤', operate: false },
                        //{ field: 'receivename', title: __('当前审批人') },
                        //{ field: 'delegatename', title: __('委托人') },
                        //{ field: 'status', title: '状态', formatter: Controller.api.formatter.status, searchList: [{ id: 1, name: '审批中' }, { id: 2, name: '已完成' },{ id: 3, name: '已取消' }] },
                         { field: 'startdate', title: __('生效日期'), operate: false, addclass: 'datetimerange'},
                       { field: 'enddate', title: __('到期日期'), operate: 'RANGE', addclass: 'datetimerange'},
                        {field: 'validstatus', title: __('协议状态'), formatter: Controller.api.formatter.validstatus, searchList: [{ id: -1, name: '未签约' }, { id: 1, name: '执行中' },{ id: 2, name: '执行完毕' },{ id: 3, name: '因故终止' }] },
                      // { field: 'createtime', title: __('createtime'), operate: false, addclass: 'datetimerange' },
                       //  { field: 'completedtime', title: __('完成时间'), operate: false, addclass: 'datetimerange' },
                       {field: 'instancestatus', title: __('申请状态'), formatter: Controller.api.formatter.status, searchList: [{ id: 1, name: '审批中' }, { id: 2, name: '已完成' },{ id: 3, name: '已取消' }] },
                     ]
                ]
            });

            $(".btn-check").click(function() {
                var ids = Table.api.selectedids(table)
                console.log(ids)
                if(ids.length>0)
                    layer.confirm("确定发起？", function() {
                        $.ajax({
                            url: "cooperate/cooperation/check",
                            data: {
                                ids: ids
                            },
                            success(res) {
                                if (res == 1) {
                                    layer.alert("发起成功！")
                                } else {
                                    layer.alert("发起失败！")
                                }
                                table.bootstrapTable('refresh', {});
                            }
                        })
                    })
                else{
                    layer.alert("请选择项目")
                }
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
			
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                status: function (value, row, index) {
                    var res = '';
                    var className = '';
                    switch (row.instancestatus) {
                        case 0:
                        case 1:
                            res = '审批中';
                            className = 'info';
                            break;
                        case 2:
                            res = '已完成';
                            className = 'success';
                            break;
                        case 3:
                            res = '已取消';
                            className = 'primary';
                            break;
                        default:
                            break;
                    }
                    return '<a href="javascript:;" class="searchit" data-toggle="tooltip" "><span class="label label-' + className + '">' + res + '</span></a>';
                },
				
				
				validstatus: function (value, row, index) {
                    var res = '';
                    var className = '';
                    switch (row.validstatus) {
                       
                        case -1:
                            res = '未签约';
                            className = 'primary';
                            break;
                        case 1:
                            res = '执行中';
                           className = 'info';
                            break;
                        case 2:
                            res = '执行完毕';
                           className = 'success';
                            break;
						case 3:
                            res = '因故终止';
                           className = 'primary';
                            break;
                        default:
                            break;
                    }
                    return '<a href="javascript:;" class="searchit" data-toggle="tooltip" "><span class="label label-' + className + '">' + res + '</span></a>';
                },
				
              
               
				type: function (value, row, index) {
                    var res = '';
                    var className = '';
                    switch (row.ctype) {
                        case "1":
							res = '校地合作';
							className = 'info';
							 break;
                        case "2":
                            res = '校企合作';
							className = 'info';
                           
                            break;
                        case "3":
                            res = '校校合作';
							className = 'info';
                           
                            break;
                        case "4":
                            res = '其他';
							className = 'info';
							 break;

                        default:
                            break;
                    }
                    return '<a href="javascript:;" class="searchit" data-toggle="tooltip" "><span class="label label-' + className + '">' + res + '</span></a>';
                },
				
                browser: function (value, row, index) {
                    return '<a class="btn btn-xs btn-browser">' + value + '</a>';
                },
            },
			
			
            events: {//绑定事件的方法
                browser: {
                    'click .btn-browser': function (e, value, row, index) {
                        e.stopPropagation();
                        var url;
                        var mode = 'view';

                        url = 'flow/cooperation/show?ids=' + row.bizobjectid + '&instanceid=' + row.id + '&mode=' + mode
                        if (url.indexOf("{ids}") !== -1) {
                            url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                        }
                        var title = '合作协议(' + row.instancecode + ')';
                        Fast.api.open(url, title, {"area":['950px','600px']});
                    }
                },
            },
            mergeCells: function (data, fieldNames, colspan, target) {
                for (let g = 0; g < fieldNames.length; g++) {
                    var fieldName = fieldNames[g];
                    if (data.length == 0) {
                        return;
                    }
                    var numArr = [];
                    var value = data[0][fieldName];
                    var num = 0;
                    for (var i = 0; i < data.length; i++) {
                        if (value != data[i][fieldName]) {
                            numArr.push(num);
                            value = data[i][fieldName];
                            num = 1;
                            continue;
                        }
                        num++;
                    }
                    if (typeof (value) !== "undefined" && value !== "") {
                        numArr.push(num);
                    }
                    var merIndex = 0;
                    for (var i = 0; i < numArr.length; i++) {
                        $(target).bootstrapTable('mergeCells', { index: merIndex, field: fieldName, colspan: colspan, rowspan: numArr[i] })
                        merIndex += numArr[i];
                    }
                }
            },
        }
    };
    return Controller;
});
